<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>上传应用</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body style="height: 750px;background-color: white">
<div style="width:80%;height:100%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="uploadApk">上传应用</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">应用名称</label>
            <div class="layui-input-block">
                <input type="text" id="apkName" name="apkName" autocomplete="off" readonly class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文件名称</label>
            <div class="layui-input-block">
                <input type="text" id="fileName" name="fileName" required  lay-verify="required" readonly autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">应用包名</label>
            <div class="layui-input-block">
                <input type="text" id="apkPackage" name="apkPackage" required  lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">应用版本</label>
            <div class="layui-input-block">
                <input type="text" id="versions" name="versions" required  lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否公开</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="publicity">
                    <option value="1">公开</option>
                    <option value="0">未公开</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>启用</div>
                <input type="radio" name="status" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>禁用</div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-upload">
                <div class="layui-input-block">
                    <div class="layui-upload-list" style="display: inline;">
                        <img class="layui-upload-img" name="apkPicture" id="apkPicture" style="width: 200px;height: 150px">
                        <p id="demoText"></p>
                    </div>
                </div>
                <input type="hidden" class="input-text" id="picture" name="picture">
                <input type="hidden" class="input-text" id="apkFile" name="apkFile">
                <input type="hidden" class="input-text" id="md5" name="md5">
                <input type="hidden" class="input-text" id="targetUrl" name="targetUrl">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="test">上传应用图片</button>
                <button class="layui-btn" id="saveApk" lay-submit lay-filter="saveApk">立即添加</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    window.onload = function(){
        $("input[name='apkName']").val(GetQueryString("apkName"));
    }

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        //添加操作
        form.on('submit(saveApk)',function(data){
            var picture = $("input[name='picture']").val();
            var apkFile = $("input[name='apkFile']").val();
            var fileName = $("input[name='fileName']").val();
            var versions = $("input[name='versions']").val();
            var md5 = $("input[name='md5']").val();
            var targetUrl = $("input[name='targetUrl']").val();
            var apkName = $("input[name='apkName']").val();
            var publicity = $("#publicity").find("option:selected").val();
            var apkPackage = $("input[name='apkPackage']").val();
            data.field.apkPicture=picture;
            data.field.publicity=publicity;
            data.field.file_name=fileName;
            data.field.md5=md5;
            data.field.target_url=targetUrl;
            data.field.versions=versions;
            data.field.apk_name=apkName;
            data.field.apk_package = apkPackage;
            if(picture==null || picture==""){
                layer.msg('请上传应用图片！');
            }else if(apkFile==null || apkFile==""){
                layer.msg('请上传应用！');
            }else{
                console.log(data.field);
                $.ajax({
                    url:"/apk/saveApk"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        console.log(data);
                        if(data.code==1){
                            layer.msg('版本号已存在！');
                        }else if(data.code==2) {
                            layer.msg('上传应用失败！');
                        }else if(data.code==3){
                            //layer.close(layer.index);
                            layer.msg('上传应用成功！');
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);//关闭弹出层
                                window.parent.location.reload();//重新加载父页面表格
                            }, 500);
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
            }
            return false;
        });

        var uploadInst = upload.render({
            elem: '#uploadApk'
            , url: "/apk/upLoadApk"
            , multiple: true
            , before: function (obj) {
            }
            , done: function (res) {
                console.log("res",res);
                //判断上传是否成功
                if (res.code == 0) {
                    $("#apkFile").val(res.data.apk_name);
                    $("#apkPackage").val(res.data.apk_package);
                    $("#md5").val(res.data.md5);
                    $("#fileName").val(res.data.file_name);
                    $("#versions").val(res.data.versions);
                    $("#targetUrl").val(res.data.target_url);
                    console.log("targetUrl",res.data.target_url);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
            , accept: 'file'
        });

        //图片上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/uploadImg' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#apkPicture').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        // 设置下拉列表的高度
        $(".layui-form-select dl").css("max-height",'400px')
    });
</script>

</body>
</html>